package and.booze.now;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class MyCanvas extends SurfaceView implements SurfaceHolder.Callback {

	private CanvasThread thread;
	private ArrayList<Dice> dices = new ArrayList<Dice>();

	public MyCanvas(Context context, AttributeSet attrs) {
		super(context, attrs);

		SurfaceHolder holder = getHolder();
		holder.addCallback(this);

		thread = new CanvasThread(holder, context, new Handler());
		setFocusable(false);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
	}
	
	public void initDices() {
		dices.add(new Dice(BitmapFactory.decodeResource(getResources(), R.drawable.dice_1), 1));
		dices.add(new Dice(BitmapFactory.decodeResource(getResources(), R.drawable.dice_2), 2));
		dices.add(new Dice(BitmapFactory.decodeResource(getResources(), R.drawable.dice_3), 3));
		dices.add(new Dice(BitmapFactory.decodeResource(getResources(), R.drawable.dice_4), 4));
		dices.add(new Dice(BitmapFactory.decodeResource(getResources(), R.drawable.dice_5), 5));
		dices.add(new Dice(BitmapFactory.decodeResource(getResources(), R.drawable.dice_6), 6));
	}

	class CanvasThread extends Thread {

		private SurfaceHolder mSurfaceHolder;
		private boolean mRun = true;
		private Resources res;

		public CanvasThread(SurfaceHolder surfaceHolder, Context context,
				Handler handler) {

			mSurfaceHolder = surfaceHolder;
			res = context.getResources();
		}

		private void doDraw(Canvas c) {
			Log.d("MyCanvas","doDraw");
			Set currentSet = doTheRolling();
			c.drawColor(Color.BLACK);
			c.drawBitmap(currentSet.getDice1().getImage(),
					100, 100, null);
			c.drawBitmap(currentSet.getDice2().getImage(),
					100, 300, null);
		}

		private Set doTheRolling() {
			Random rnd = new Random();
			Set currentSet = new Set(dices.get(rnd.nextInt(6)), dices.get(rnd.nextInt(6)));
			return currentSet;
		}
		
		@Override
		public void run() {
			// the thread
			while (mRun) {

				Canvas c = null;
				try {
					c = mSurfaceHolder.lockCanvas(null);
					synchronized (mSurfaceHolder) {
						doDraw(c); // draw the canvas
					}
				} finally {
					if (c != null) {
						mSurfaceHolder.unlockCanvasAndPost(c);
					}
				}

				try {
					sleep(20);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}

class Set {
	private Dice dice1;
	private Dice dice2;
	
	public Set(Dice dice1, Dice dice2) {
		this.dice1 = dice1;
		this.dice2 = dice2;
	}
	
	public int getValue() {
		return (dice1.getValue() + dice2.getValue());
	}
	
	public Dice getDice1() {
		return dice1;
	}
	
	public Dice getDice2() {
		return dice2;
	}
}

class Dice {
	private Bitmap image;
	private int value;

	public Dice(Bitmap image, int value) {
		this.image = image;
		this.value = value;
	}
	
	public Bitmap getImage() {
		return image;
	}
	
	public int getValue() {
		return value;
	}
}